<?php

namespace App\Repositories\School;

use App\Exceptions\ApiException;
use App\Models\SchoolRoles;
use App\Models\SchoolRoleUsers;

class Check
{
    /**
     * 是否可以关注或者取消关注
     *
     * @param int $studentId
     * @param int $teacherId
     *
     * @return bool
     * @throws ApiException
     */
    public static function canLike(int $studentId, int $teacherId)
    {
        $schoolId = SchoolRoleUsers::where([
            "user_id" => $studentId,
            'role_id' => SchoolRoles::STUDENT_ROLE,
        ])->value('school_id');
        if (! $schoolId) {
            throw new ApiException("Permission denied");
        }
        $teacher = SchoolRoleUsers::where([
            'user_id' => $teacherId,
            'school_id' => $schoolId,
        ])->whereIn('role_id', [
            SchoolRoles::MANAGER_ROLE,
            SchoolRoles::TEACHER_ROLE,
        ])->first();
        if (! $teacher) {
            throw new ApiException("The teacher does not exist");
        }

        return true;
    }
}